###############################################################################
#
# Copyright (c) 2011 Rainmaker Entertainment
# All Rights Reserved.
#
# This file contains unpublished confidential and proprietary
# information of Rainmaker Entertainment.  The contents of this file
# may not be copied or duplicated, in whole or in part, by any
# means, electronic or hardcopy, without the express prior
# written permission of Rainmaker Entertainment.
#
#    $HeadURL: http://pt/src/trunk/maya/outsource/rnkOutsourceChecker/operations/common.py $
#    $Revision: 67856 $
#    $Author: wmakari $
#    $Date: 2013-09-17 18:23:18 -0700 (Tue, 17 Sep 2013) $
#
###############################################################################

###############################################################################
## INFOS
###############################################################################

# Geo checks


###############################################################################
## MODULES
###############################################################################

import traceback
import re

try :
    import maya.cmds as cmds
    import maya.mel as mel
    import maya.OpenMaya as OpenMaya
except :
    traceback.print_exc()
    print "Can't load Maya modules."
    
    
import rnkAssetChecks.common as rnkAssetChecksCommon
reload(rnkAssetChecksCommon)


import core
reload(core)
        


###############################################################################
## FUNCTIONS
###############################################################################  

def checkForNonUniqueNames(assetRoot, *args, **kw):
	
	issues = rnkAssetChecksCommon.checkForNonUniqueNames(assetRoot)

	if len(issues) == 0 :      
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="Those transforms have non-unique names", nodes=issues)

checkForNonUniqueNames.modifiedScene = False
checkForNonUniqueNames.uiName        = 'Check Non-Unique Names'
checkForNonUniqueNames.revision      = "$Revision: 67856 $".split()[1]
checkForNonUniqueNames.__doc__       = rnkAssetChecksCommon.checkForNonUniqueNames.__doc__


def checkAssetRootIsIdNode(assetRoot, *args, **kw):

    idNodeShapes = cmds.listRelatives(assetRoot, type='idNode')
    

    if not len(idNodeShapes) == 0 :    
        return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
        return core.checkResult(allowPublish=False, msg="The Asset root node is not an idNode", nodes=[assetRoot])

checkAssetRootIsIdNode.modifiedScene = False
checkAssetRootIsIdNode.uiName        = 'Check if asset root is an idNode'
checkAssetRootIsIdNode.revision      = "$Revision: 67856 $".split()[1]


def checkIdNodeScale(assetRoot, *args, **kw):

	issues = rnkAssetChecksCommon.checkIdNodeScale(assetRoot)

	if issues is True :      
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="The root node have non default scale", nodes=[assetRoot])

checkIdNodeScale.modifiedScene = False
checkIdNodeScale.uiName        = 'Check Root Node Scale'
checkIdNodeScale.revision      = "$Revision: 67856 $".split()[1]
checkIdNodeScale.__doc__       = rnkAssetChecksCommon.checkIdNodeScale.__doc__


def checkIdNodeTranslation(assetRoot, *args, **kw):

	issues = rnkAssetChecksCommon.checkIdNodeTranslation(assetRoot)

	if issues is True :      
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="The root node have non default translation", nodes=[assetRoot])

checkIdNodeTranslation.modifiedScene = False
checkIdNodeTranslation.uiName        = 'Check Root Node Translation'
checkIdNodeTranslation.revision      = "$Revision: 67856 $".split()[1]
checkIdNodeTranslation.__doc__       = rnkAssetChecksCommon.checkIdNodeTranslation.__doc__


def checkIdNodeRotation(assetRoot, *args, **kw):

	issues = rnkAssetChecksCommon.checkIdNodeRotation(assetRoot)

	if issues is True :      
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="The root node have non default rotation", nodes=[assetRoot])

checkIdNodeRotation.modifiedScene = False
checkIdNodeRotation.uiName        = 'Check Root Node Rotation'
checkIdNodeRotation.revision      = "$Revision: 67856 $".split()[1]
checkIdNodeRotation.__doc__       = rnkAssetChecksCommon.checkIdNodeScale.__doc__


def checkNamespace(assetRoot, *args, **kw):
	
	issues = rnkAssetChecksCommon.checkNamespace(assetRoot)

	if issues is True :      
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="Can't remove the namespace, check the script editor for details", nodes=[])

checkNamespace.modifiedScene = True
checkNamespace.uiName        = 'Remove Namespaces'
checkNamespace.revision      = "$Revision: 67856 $".split()[1]
checkNamespace.__doc__       = rnkAssetChecksCommon.checkNamespace.__doc__



def checkASCII(assetRoot, *args, **kw):
	""" 
	@brief Check every node names are in ASCII.
	@return : List
	
	ARGS :
			@param assetRoot - String - Asset root node.
	"""
	
	issues = []
	
	getNodes = cmds.ls()
	for eachnode in getNodes :
		try :
			eachnode.decode('ascii')
		except UnicodeDecodeError:
			issues.append(eachnode)
	

	if len(issues) == 0 :    
		return core.checkResult(allowPublish=True, msg="Everything is OK")
	else :           
		return core.checkResult(allowPublish=False, msg="Those nodes contain non-ASCII characters", nodes=issues)

checkASCII.modifiedScene = False
checkASCII.uiName = 'Check non-ASCII nodes'
checkASCII.revision = "$Revision: 67856 $".split()[1]



def checkUnit(assetRoot, *args, **kws):
    """check maya working units:
    Linear: centimeter
    Angular: degrees
    Time: Film(24fps)
    """

    if cmds.currentUnit(q=True, l=True) != "cm" or cmds.currentUnit(q=True, a=True) != "deg" or cmds.currentUnit(q=True, t=True) != "film":
        return core.checkResult(allowPublish=False, msg="Maya working unit should be: Linear: centimeter, Angular: degrees, Time: Film(24fps)", nodes=list())
    else:
        return core.checkResult(allowPublish=True, msg="Everything is OK")

checkUnit.modifiedScene = False
checkUnit.uiName = 'Check Maya Working Unit'
checkUnit.revision = "$Revision: 67856 $".split()[1]
